﻿package net.wangit.view.element.component;

import java.util.List;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.table.Column;
import net.wangit.jdbc.table.TableMeta;
import net.wangit.jdbc.table.TableMetaFactory;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.util.NumberUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.util.Html;


public class FileElementWriter extends AbstractElementWriter {

	public FileElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		UnitComponent unit = ec.getParent();
		String tableName = unit.getTableName();
		String fileName = null;
		if (row != null)
			fileName = row.get((new StringBuilder(String.valueOf(ec.getId())))
					.append("filename").toString());
		if (Checker.isEmpty(tableName) || Checker.isEmpty(fileName))
			return;
		html.append("<a href=\"/").append(unit.getDomain()).append("/").append(
				unit.getId()).append("/").append(ec.getId()).append(".do?");
		String domain = unit.getDomain();
		TableMeta table = TableMetaFactory.getTableMeta(domain, tableName);
		List<Column> columns = table.getPrimaryKey().getColumns();
		for (int i = 0; i < columns.size(); i++) {
			Column column = columns.get(i);
			String columnName = column.getName();
			if (i > 0)
				html.append("&");
			html.append(columnName).append("=").append(row.get(columnName));
		}

		html.append("\">").append(fileName);
		String fileSize = row
				.get((new StringBuilder(String.valueOf(ec.getId()))).append(
						"filesize").toString());
		if (!Checker.isEmpty(fileSize))
			html.append(" (").append(getK(fileSize)).append("K)");
		html.append("</a>");
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, false);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		String fileName = null;
		String fileSize = null;
		if (row != null) {
			fileName = row.get((new StringBuilder(String.valueOf(ec.getId())))
					.append("filename").toString());
			fileSize = row.get((new StringBuilder(String.valueOf(ec.getId())))
					.append("filesize").toString());
		}
		if (!Checker.isEmpty(fileName))
			html.append(fileName);
		if (!Checker.isEmpty(fileSize))
			html.append(" (").append(getK(fileSize)).append("K)");
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<input disabled type=\"file\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E25");
		Html.setStyleWithWidth(html, ec.getWidth());
		html.append("/>");
		appendValue(html, ec, row);
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControlWithDisplay(ac, html, ec, row, true);
	}

	private void writeControlWithDisplay(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean hidden) {
		html.append("<input type=\"file\" name=\"").append(
				ElementUtil.getAlias(ec)).append("\"");
		if (hidden)
			html.append(" style=\"display:none\"");
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E25");
		Html.setStyleWithWidth(html, ec.getWidth());
		html.append("/>");
		appendValue(html, ec, row);
	}

	private String getK(String fileSize) {
		double size = Double.parseDouble(fileSize);
		double k = NumberUtil.format1(size / 1024D);
		return Double.toString(k);
	}

	private void appendValue(StringBuffer html, ElementComponent ec, Row row) {
		if (row != null) {
			String filedName = ElementUtil.getAlias(ec);
			html.append("<input type=\"hidden\" name=\"").append(filedName)
					.append(".f").append("\"");
			Html.setValue(html, row.get((new StringBuilder(String.valueOf(ec
					.getId()))).append("filesize").toString(), ""));
			html.append("/>");
		}
	}
}
